package com.miyake.loveviewweb.client.community;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.client.community.FriendListWidget.FriendListType;
import com.miyake.loveviewweb.client.dialog.BasicDialog;
import com.miyake.loveviewweb.client.dialog.TextDialog;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.shared.model.GroupModel;

public class CommunityMainUI extends Composite {

    private IGroupList groupList;
    private VerticalPanel mainPanel;
    private VerticalPanel contentPanel;
    
    public CommunityMainUI() {
        ScrollPanel scrollPanel = new ScrollPanel();
        scrollPanel.setHeight("90%");
        mainPanel = new VerticalPanel();
        scrollPanel.add(mainPanel);
        mainPanel.setWidth("100%");
        mainPanel.setHeight("100%");
        initWidget(scrollPanel);
        
        contentPanel = new VerticalPanel();
        contentPanel.setWidth("100%");
        //contentPanel.setHeight("70%");
        
        MenuBar menuBar = new MenuBar();
        MenuItem menuItem = new MenuItem("", new Command() {

            public void execute() {

            }
            
        });
        menuBar.addItem(menuItem);
        mainPanel.add(menuBar);
        mainPanel.setStylePrimaryName("community");
        mainPanel.add(contentPanel);
        
        initPanel();

    }

    private void initPanel() {
        contentPanel.clear();
        contentPanel.add(getGroupListPanel());
        contentPanel.add(getFriendsPanel());
    }
    
    private Widget getGroupListPanel() {
        VerticalPanel groupListPanel = new VerticalPanel();
        groupListPanel.setWidth("100%");
        HorizontalPanel toolBar = new HorizontalPanel();
        groupListPanel.add(new HTML("<h2>Group</h2>"));
        groupListPanel.add(toolBar);
        groupList = new GroupListTable(IGroupList.GroupListType.MyGroup, new GroupListHandler() {

            public void onSelect(GroupModel groupModel) {
                contentPanel.clear();
                contentPanel.add(new GroupPropertyPanel(groupModel) {

                    @Override
                    protected void onBack() {
                        initPanel();
                    }
                    
                });
            }
            
        });
        groupListPanel.add(groupList.getComposite());
        groupList.update();
        
        toolBar.add(new Button("Search", new ClickHandler() {

            public void onClick(ClickEvent event) {
                showGroupSearchDialog();
            }
            
        }));
        toolBar.add(new Button("Create", new ClickHandler() {

            public void onClick(ClickEvent event) {
                createGroup(groupList);
            }
            
        }));
        return groupListPanel;
    }

    protected void createGroup(final IGroupList groupList) {
        new BasicDialog(new TextDialog()) {

            @Override
            protected void onOK(Composite composite) {
                String name = ((TextDialog)composite).getText();
                DataService.Util.getInstance().createGroup(name, new AsyncCallback<Key>() {

                    public void onFailure(Throwable caught) {
                        // TODO Auto-generated method stub
                        
                    }

                    public void onSuccess(Key result) {
                        groupList.update();
                    }
                    
                });
            }
            
        }.showDialog();
        

    }

    private VerticalPanel getFriendsPanel() {
        VerticalPanel p = new VerticalPanel();
        HorizontalPanel toolBar = new HorizontalPanel();
        
        p.add(new HTML("<h2>Friends</h2>"));
        p.add(toolBar);
        
        Button searchButton = new Button("Search");
        toolBar.add(searchButton);
        searchButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                showFriendSearchDialog();
            }
        });
        
        p.add(new FriendListWidget(FriendListType.MyFriends));          
        return p;
    }

    protected void showFriendSearchDialog() {
        new BasicDialog(new FriendSearchPanel()) {
            @Override
            protected boolean isOkVisible() {
                return false;
            }
            
            @Override
            protected void onOK(Composite composite) {
                initPanel();
            }
            
        }.showDialog();
    }

    protected void showGroupSearchDialog() {
        new BasicDialog(new GroupSearchPanel()) {

            @Override
            protected boolean isOkVisible() {
                return false;
            }

            @Override
            protected void onOK(Composite composite) {
                GroupSearchPanel panel = (GroupSearchPanel)composite;
                DataService.Util.getInstance().joinGroup(panel.getSelectedGroup().getKey(), new AsyncCallback<Void>() {

                    public void onFailure(Throwable caught) {
                        Window.alert(caught.getMessage());
                    }

                    public void onSuccess(Void result) {
                        initPanel();
                    }
                    
                });
            }
            
        }.showDialog();
    }
}
